package com.shuaiche.yeguan.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.model.SCUpdateResponse;


public class DialogUtil {

    public static AlertDialog getAlertDialog(Context context, View paramView, boolean paramBoolean) {
        AlertDialog localAlertDialog = new AlertDialog.Builder(context).create();
        localAlertDialog.show();
        localAlertDialog.setCancelable(paramBoolean);
        localAlertDialog.getWindow().setContentView(paramView);
        return localAlertDialog;
    }

    public static AlertDialog getAlertDialog(Context context, CharSequence msg, CharSequence btName,
                                             DialogInterface.OnClickListener onclickListener) {
        final AlertDialog alert = new AlertDialog.Builder(context).create();
        alert.setMessage(msg);
        alert.setCancelable(false);
        alert.setButton(DialogInterface.BUTTON_POSITIVE, btName, onclickListener);
        return alert;
    }



    public static void getAlertDialog(Context context, String title, String msg, String commitName,
                                             String cancelName, DialogInterface.OnClickListener onClick) {
        AlertDialog alert = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setCancelable(false);
        builder.setPositiveButton(commitName, onClick);
        builder.setNegativeButton(cancelName, (dialog, which) -> dialog.dismiss());
        alert = builder.create();
        alert.show();
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ResourceUtils.getColor(context, R.color.text_blue));
    }

    public static void getAlertDialog(Context context, String title, String msg, String commitName,
                                             DialogInterface.OnClickListener onClick) {
        AlertDialog alert = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setCancelable(false);
        builder.setPositiveButton(commitName, onClick);
        alert = builder.create();
        alert.show();
        alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ResourceUtils.getColor(context, R.color.text_blue));
    }

    public static ProgressDialog getProgressDialog(Context context, String msg,
                                                   boolean isCancelable) {// 显示进度条
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setIndeterminate(false);
        progressDialog.setMessage(msg);
        progressDialog.setCancelable(isCancelable);
        return progressDialog;
    }


    public static void getUpdateDialog(final Activity context, final String mVersionMsg,
                                       final int type) {

//        final String downPath = "/storage/emulated/0/Android/data/com.shuaiche.sc/cache";

        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View
                .inflate(context, R.layout.sc_dlg_update, null);
        builder.setView(view);
        builder.setCancelable(false);
        Button cancel= (Button) view.findViewById(R.id.btnCancel);//取消按钮
        Button update = (Button) view.findViewById(R.id.btnUpdate);
        TextView content = (TextView) view.findViewById(R.id.tvContent);
        content.setText(mVersionMsg);

        if (type == 1) { //强制升级
            cancel.setVisibility(View.GONE);
        } else {        //0非强制
            cancel.setVisibility(View.VISIBLE);
        }


        final AlertDialog dialog = builder.create();
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialogInfer != null){
                    dialogInfer.updateClick();
                }
                dialog.dismiss();
            }
        });
        if (!dialog.isShowing() && !context.isFinishing() && !context.isDestroyed()){
            dialog.show();
        }
    }

    public static DialogInfer dialogInfer;

    public interface DialogInfer{
        void updateClick();
    }


}
